/** @odoo-module */

import { AbstractSpreadsheetAction } from "@spreadsheet_edition/bundle/actions/abstract_spreadsheet_action";
import { registry } from "@web/core/registry";
import { _t } from "@web/core/l10n/translation";
import { waitForDataLoaded } from "@spreadsheet/helpers/model";




import { useSubEnv } from "@odoo/owl";

export class FssEditAction extends AbstractSpreadsheetAction {
    resModel = "spreadsheet.excel";
    notificationMessage = _t("New dashboard created");

    setup() {
        super.setup();
        useSubEnv({
            makeCopy: this.makeCopy.bind(this),
            convertToRecords: this.convertToRecords.bind(this),
        });
    }

    /**
    * Spreadsheet convert to record.
    */
    updateExportData(sheet_name, data, col, row, content) {
        let f = parseInt(col / 26);
        let s = col % 26;
        let cell;
        if (f > 0 && s > 0) {
            cell = String.fromCharCode(64 + f) + String.fromCharCode(64 + s) + String(row);
        } else if (f > 0 && s == 0) {
            cell = String.fromCharCode(64 + f) + String(row);
        } else if (f == 0 && s > 0) {
            cell = String.fromCharCode(64 + s) + String(row);
        }

        let i = 0;
        for (const sheet of data["sheets"]) {
            if (sheet["id"] == sheet_name) {
                if (data["sheets"][i]["cells"][cell] != undefined) {
                    if (data["sheets"][i]["cells"][cell]['content'] != undefined) {
                        data["sheets"][i]["cells"][cell]['content'] = content;
                    }
                }

            }
            i = i + 1;
        }

    }

    async convertToRecords() {
        const data = this.model.exportData();
        await waitForDataLoaded(this.model);

        for (const sheet_id of this.model.getters.getSheetIds()) {
            for (const cell of Object.values(this.model.getters.getCells(sheet_id))) {
                const { col, row } = this.model.getters.getCellPosition(cell.id);
                let value;

                if (cell.isFormula) {
                    value = this.model.getters.evaluateFormula(sheet_id, cell.content).toString();
                    this.updateExportData(sheet_id, data, col + 1, row + 1, value);
                } else {
                    value = cell.content;
                }
            }
        }

        await this.orm.write("spreadsheet.excel", [this.resId], {
            raw_no_formula: JSON.stringify(data),
            mimetype: "application/o-spreadsheet",
        });
    }

    async _onSpreadSheetNameChanged(detail) {
        await super._onSpreadSheetNameChanged(detail);
        const { name } = detail;
        await this.orm.write("spreadsheet.excel", [this.resId], {
            name,
        });
    }

    async shareSpreadsheet(data, excelExport) {
        const url = await this.orm.call("spreadsheet.dashboard.share", "action_get_share_url", [
            {
                dashboard_id: this.resId,
                spreadsheet_data: JSON.stringify(data),
                excel_files: excelExport.files,
            },
        ]);
        return url;
    }

    getSaveData() {
        const data = this.model.exportData();
        this.orm.write("spreadsheet.excel", [this.resId], {
            raw: JSON.stringify(data),
            mimetype: "application/o-spreadsheet",
        });
        return {
            data,
            revisionId: data.revisionId,
            thumbnail: this.getThumbnail(),
        };
    }
}

FssEditAction.template = "spreadsheet_excel.FssEditAction";
registry.category("actions").add("action_edit_fss", FssEditAction, { force: true });
